package com.umpay.privi.util;

import java.io.IOException;
import com.umpay.privi.common.Constants;

public class SerialUtil {

	public static String getSerial() {
		return getSerialFromFile("ID", 16);
	}

	public static String getSerialFromFile(String serialName, int length) {
		long serialNo = Serial.next(serialName);
		String serialNoStr = String.valueOf(serialNo);
		return formatSerial(serialNoStr, length);
	}
	
	public static String getSerialFromFile(String filePath, String serialName, int length) {
		long serialNo = Serial.next(serialName,filePath);
		String serialNoStr = String.valueOf(serialNo);
		return formatSerial(serialNoStr, length);
	}

	static String formatSerial(String serialNo, int length) {
		Integer prefixNum = length - serialNo.length();
		if (prefixNum < 0)
			throw new IllegalStateException();
		StringBuffer prefixString = new StringBuffer();
		for (int i = 0; i < prefixNum; i++) {
			prefixString.append("0");
		}
		return prefixString.toString() + serialNo;
	}
	
	public static String  getRpid(String seqKey,int length) {
		long id = SequenceUtil.getInstance().getSequence4File(Constants.RPID_SEQ);
		String seq= SequenceUtil.formatSequence(id, length);
		
		return seq;
	}

	public static void main(String args[]) throws IOException {
		System.out.println(getSerial());
	}
}
